/******************************/
/*        立体匹配和测距        */
/******************************/
#include <Windows.h>
#include "cvstereo.h"
Point origin;
Rect selection;
bool selectObject= false;
Mat xyz;
static void onMouse(int envent,int x,int y,int, void*)
{
    if (selectObject)
    {
        selection.x=MIN(x,origin.x);
        selection.y=MIN(y,origin.y);
        selection.width=abs(x-origin.x);
        selection.height=abs(y-origin.y);

    }
    switch (envent)
    {
        case EVENT_LBUTTONDOWN:
            origin=Point(x,y);
            selection=Rect(x,y,0,0);
            selectObject= true;
            cout<<origin<<"in the world coordinate is: "<<xyz.at<Vec3f>(origin)<<endl;
            break;
        case EVENT_LBUTTONUP:
            selectObject= false;
            if(selection.width>0 && selection.height>0)
            break;

    }
}

int main()
{

    CVStereo* cvstereo = new CVStereo;
    vector<Mat> left_imgs;
    left_imgs.push_back(imread("left01.jpg"));
    left_imgs.push_back(imread("left02.jpg"));
    left_imgs.push_back(imread("left03.jpg"));
    left_imgs.push_back(imread("left04.jpg"));
    vector<Mat> right_imgs;
    right_imgs.push_back(imread("right01.jpg"));
    right_imgs.push_back(imread("right02.jpg"));
    right_imgs.push_back(imread("right03.jpg"));
    right_imgs.push_back(imread("right04.jpg"));
    cvstereo->stereoCalibCalculate(left_imgs, right_imgs, Size(9, 6));
    namedWindow("undistort");
    imshow("src", left_imgs.at(0));
    namedWindow("undistort");
    imshow("undistort", cvstereo->rectify_undistort(left_imgs.at(0)));
    namedWindow("remap");
    imshow("remap", cvstereo->rectify_remap(left_imgs.at(0)));
//    cvstereo->stereoRectifyCalculate(left_imgs, right_imgs);
//    namedWindow("disparity");
//    setMouseCallback("disparity",onMouse);
//    cvstereo->stereo_SGBM_match(xyz);
    waitKey(0);
    destroyAllWindows();
    delete cvstereo;
    return 0;
}
